package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.untils.QiniuUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Author Administrator
 * @Date 2020/3/31 0031
 * @Version 1.0
 * @Description 预约套餐控制器
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    /**
     * 依赖注入
     */
    @Reference
    private SetmealService setmealService;

    /***
     * 分页查询setmeal/findPage
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = setmealService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        System.out.println(page);
        return page;
    }

    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupId) {
        try {
//        存储基本信息数据 t_setmeal
            setmealService.add(setmeal, checkgroupId);
//            成功返回
            return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
//            失败返回添加失败
            return new Result(true,MessageConst.ADD_SETMEAL_FAIL);
        }
    }
    /**
     * 文件上传
     * @param imgFile 页面传入的图片
     * @return
     */
    @RequestMapping("upload")
    public Result upload(MultipartFile imgFile){
        try {
            //1.获取原图片的名字
            String originalFilename = imgFile.getOriginalFilename();
            //2.处理上传的图片名称 (UUID + 原图片名称 组合方式)
            String uploadName = UUID.randomUUID().toString().replace("-","") + "_" + originalFilename;
            //3.向七牛云空间中 上传图片
            QiniuUtils.upload2QiNiu(imgFile.getBytes(), uploadName);
            //4.把上传后的图片 在七牛云空间的路径地址, 返回给前端页面
            String imageUrl = QiniuUtils.qiniu_img_url_pre + uploadName;
            //控制台打印
            System.out.println("imageUrl = " + imageUrl);

            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, imageUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }

    }



}
